
package com.mrd.gtimpframe.action;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mrd.base.Jqgrid;
import com.mrd.framework.config.web.json.annotation.JField;
import com.mrd.framework.config.web.json.annotation.Json;
import com.mrd.gtimpframe.entity.CompanyInfo;
import com.mrd.gtimpframe.entity.SuperCompanyInfo;
import com.mrd.gtimpframe.entity.TGenComContact;
import com.mrd.framework.config.web.json.Result;
import com.mrd.gtimpframe.service.ComAuditService;
import com.mrd.gtimpframe.service.CompanyInfoService;
import com.mrd.gtimpframe.vo.ComInfoAuditVO;

import com.mrd.util.FormBeanUtil;
import com.mrd.util.JsonBinder;
import com.mrd.util.R;
import com.mrd.util.ResponseUtils;
import org.apache.commons.lang3.StringUtils;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RequestMapping("/business/cominfo")
@Controller
public class ComInfoAuditController {
    @Autowired
    private ComAuditService comAuditService;
    @Autowired
    private CompanyInfoService companyInfoService;

    /**
     * 管理员审核列表，是审核模块的入口
     */
    @RequestMapping("/auditMain")
    public String auditMain(){
        return "/gtimpframe/cominfo/auditMain";
    }

    /**
     * 审核模块的表格数据
     */
    @RequestMapping("/auditMainData")
    @ResponseBody
    public Page auditMainData(Jqgrid jqgrid){
        return comAuditService.auditGridData(jqgrid);
    }

    /**
     * 进行公司审核的页面
     */
    @RequestMapping("/toAuditCom")
    public String toAuditCom(){
        return "/gtimpframe/cominfo/auditCom";
    }

    /**
     * 进行公司审核的页面
     */
    @RequestMapping("/auditComData")
    @ResponseBody
    public ComInfoAuditVO auditComData(String registerType, String auditId){
        return comAuditService.getAuditData(registerType, auditId);
    }

    /**
     * 进行审核
     */
    @RequestMapping("/didComAudit")
    @ResponseBody
    public Result didComAudit(String registerType, String auditId, boolean pass, String remark){
        return comAuditService.didComAudit(registerType, auditId, pass, remark);
    }

    /**
     * 获取所有待审核客户数量，待审核检查包括以下两点：
     * 1、直接注册的待审核数量
     * 2、隐性客户注册的待审核数量
     */
    @RequestMapping("/getCountOfWaitingAudit")
    @ResponseBody
    public int getCountOfWaitingAudit(){
        return 0;
    }

    /**
     * 管理员类型转换页面
     * @author han
     */
    @RequestMapping("/comInfoChangePage")
    public String ComInfoChangeListPage(){
        return "/gtimpframe/orderFor/tGenCompanyInfoChange";
    }

    /**
     * 列表
     * @author han
     */
    @RequestMapping("/getComInfoChangeData")
    public void getComInfoChangeData(com.mrd.framework.ui.Jqgrid jqgrid, HttpServletResponse response) throws Exception {
        String result = "";
        Pageable page = jqgrid.getPage();
        int pageNumber = page.getPageNumber();
        int pageSize = page.getPageSize();

        // 分页
        PageHelper.startPage(pageNumber + 1, pageSize);

        // 排序
        Sort.Order order = jqgrid.getOrder();
        if (order != null) {
            String direction = order.getDirection().toString();
            String propertyName = order.getProperty();
            PageHelper.orderBy(propertyName + " " + direction);
        }
        SuperCompanyInfo superCompanyInfo = FormBeanUtil.getEntityFromJqgrid(jqgrid, SuperCompanyInfo.class);
        //处理公司类型查询
        if(superCompanyInfo!=null && StringUtils.isNotEmpty(superCompanyInfo.getGsType())){
            if(!superCompanyInfo.getGsType().equals("0") && !superCompanyInfo.getGsType().equals("9")){
                superCompanyInfo.setIsDrectCust("");
                if(superCompanyInfo.getGsType().equals("1")){//代录单人
                    superCompanyInfo.setType("CL");
                    superCompanyInfo.setIsAgent("1");
                }else if(superCompanyInfo.getGsType().equals("2")){//客户
                    superCompanyInfo.setType("CL");
                    superCompanyInfo.setIsAgent("0");
                }
            }else{
                superCompanyInfo.setIsDrectCust(superCompanyInfo.getGsType());
            }
        }

        List<CompanyInfo> companyList = companyInfoService.selCompanyInfoChangeList(superCompanyInfo);
        PageInfo<CompanyInfo> pi = new PageInfo<CompanyInfo>(companyList);
        Page<Map<String, Object>> pMap = new PageImpl(companyList, page, pi.getTotal());
        JsonBinder jsonBinder = JsonBinder.buildNormalBinder();
        jsonBinder.setDateFormat("yyyy-MM-dd HH:mm:ss");
        result = jsonBinder.toJson(pMap);
        ResponseUtils.renderJson(response, result);
    }

    /**
     * 转换客户类型
     * @author han
     * @param superCompanyInfo
     * @return
     */
    @RequestMapping("/changeCompanyType")
    @ResponseBody
    public R changeCompanyType(SuperCompanyInfo superCompanyInfo) {
        try {
            if(superCompanyInfo!=null && StringUtils.isNotEmpty(superCompanyInfo.getGsType())){

                if(superCompanyInfo.getGsType().equals("0")){//隐性客户
                    superCompanyInfo.setIsDrectCust("0");
                    superCompanyInfo.setIsAgent("0");
                }else if(superCompanyInfo.getGsType().equals("9")){//混合型客户
                    superCompanyInfo.setIsDrectCust("9");
                    superCompanyInfo.setIsAgent("1");
                }else{
                    if(superCompanyInfo.getGsType().equals("1")){//代录单人
                        superCompanyInfo.setIsAgent("1");
                        superCompanyInfo.setIsDrectCust("1");
                    }else if(superCompanyInfo.getGsType().equals("2")){//客户
                        superCompanyInfo.setIsAgent("0");
                        superCompanyInfo.setIsDrectCust("1");
                    }
                }
            }
            companyInfoService.changeCompanyInfoType(superCompanyInfo);
            return R.ok();
        } catch (Exception ex) {
            return R.error(ex.getMessage());
        }
    }
}
